#include "LayerGroup.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "deprecated/CCDeprecated.h"
#include "ui/UIHelper.h"
#include "base/CCConsole.h"

#include "logic/LLogicGroup.h"
#include "common/PIOStreamOperator.h"

USING_NS_CC;
using namespace ui;


CLayerGroup::CLayerGroup(void) : m_ListIndex(GLIMyGroup)
{
}


CLayerGroup::~CLayerGroup(void)
{
}

bool CLayerGroup::init() {
	// 1. super init first
	if ( !CLayerBase::init() )
	{
		return false;
	}

	m_Group = CSLoader::createNode("Group.csb");

	//ui::Button* tLoginB = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Pack, 93);
	//tLoginB->addTouchEventListener(this, toucheventselector(CLayerGroup::onLogin));

	addChild(m_Group);

	return true;
}

void CLayerGroup::onEnter() {
	CLayerBase::onEnter();

	m_Close = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Group, 92);
	m_Close->addTouchEventListener(this, toucheventselector(CLayerGroup::onClose));

	m_CreateGroup = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Group, 88);
	m_CreateGroup->addTouchEventListener(this, toucheventselector(CLayerGroup::onCreateGroup));

	m_ApplyGroup = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Group, 89);
	m_ApplyGroup->addTouchEventListener(this, toucheventselector(CLayerGroup::onApplyGroup));

	m_AgreeGroup = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Group, 90);
	m_AgreeGroup->addTouchEventListener(this, toucheventselector(CLayerGroup::onAgreeGroup));

	m_LeaveGroup = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Group, 91);
	m_LeaveGroup->addTouchEventListener(this, toucheventselector(CLayerGroup::onLeaveGroup));

	m_MyGroup = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Group, 93);
	m_MyGroup->addTouchEventListener(this, toucheventselector(CLayerGroup::onMyGroup));

	m_ListGroup = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Group, 94);
	m_ListGroup->addTouchEventListener(this, toucheventselector(CLayerGroup::onListGroup));

	m_TempUser = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Group, 95);
	m_TempUser->addTouchEventListener(this, toucheventselector(CLayerGroup::onTempUser));

	m_GetList = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Group, 96);
	m_GetList->addTouchEventListener(this, toucheventselector(CLayerGroup::onGetList));

	m_GroupList = (ui::ListView*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Group, 80);
}

void CLayerGroup::FlushMyGroup() {
	if (m_ListIndex == GLIMyGroup)
		m_Director->getScheduler()->performFunctionInCocosThread(CC_CALLBACK_0(CLayerGroup::onFlushMyGroup, this));
}

void CLayerGroup::FlushListGroup() {
	if (m_ListIndex == GLIListGroup)
		m_Director->getScheduler()->performFunctionInCocosThread(CC_CALLBACK_0(CLayerGroup::onFlushListGroup, this));
}

void CLayerGroup::FlushTempUser() {
	if (m_ListIndex == GLITempUser)
		m_Director->getScheduler()->performFunctionInCocosThread(CC_CALLBACK_0(CLayerGroup::onFlushTempUser, this));
}

/*
void CLayerGroup::onExit() {
	CLayerBase::onExit();
}

void CLayerGroup::update(float delta) {
	CLayerBase::update(delta);
}
*/

void CLayerGroup::onClose(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:
		this->setVisible(false);
		break;
	}
}

void CLayerGroup::onCreateGroup(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:
		{
			CGLGroup* tGroup = m_LogicData->GetGroup();
			if (nullptr == tGroup) {
				CLLogicGroup tLogic;
				tLogic.Create();
			} else {
				CCLOG("!!!!!!!!!!!!!!!!!!!!!%s", __FUNCTION__);
			}
		}

		break;
	}
}

void CLayerGroup::onApplyGroup(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:
		if (m_ListIndex == GLIListGroup) {
			cocos2d::ui::Widget* tItem = m_GroupList->getItem(m_GroupList->getCurSelectedIndex());
			AnimalLookInfo* tInfo = (AnimalLookInfo*)tItem->getUserData();
			CLLogicGroup tLogic;
			tLogic.Apply(tInfo->m_Uid);
		}
		break;
	}
}

void CLayerGroup::onAgreeGroup(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:
		if (m_ListIndex == GLITempUser) {
			ssize_t tCurIndex = m_GroupList->getCurSelectedIndex();
			cocos2d::ui::Widget* tItem = m_GroupList->getItem(tCurIndex);
			AnimalLookInfo* tInfo = (AnimalLookInfo*)tItem->getUserData();
			CLLogicGroup tLogic;
			tLogic.Agree(tInfo->m_Uid, tCurIndex);

			m_GroupList->removeItem(tCurIndex);
		}
		break;
	}
}

void CLayerGroup::onLeaveGroup(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:
		{
			CGLGroup* tGroup = m_LogicData->GetGroup();
			if (nullptr != tGroup) {
				CLLogicGroup tLogic;
				tLogic.Leave();
			} else {
				CCLOG("!!!!!!!!!!!!!!!!!!!!!%s", __FUNCTION__);
			}
		}

		break;
	}
}

void CLayerGroup::onMyGroup(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:
		m_ListIndex = GLIMyGroup;
		onFlushMyGroup();
		break;
	}
}

void CLayerGroup::onListGroup(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:
		m_ListIndex = GLIListGroup;
		onFlushListGroup();
		break;
	}
}

void CLayerGroup::onTempUser(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:
		m_ListIndex = GLITempUser;
		onFlushTempUser();
		break;
	}
}

void CLayerGroup::onGetList(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:
		CLLogicGroup tLogic;
		tLogic.GetList();
		break;
	}
}

void CLayerGroup::onFlushMyGroup() {
	m_GroupList->removeAllItems();
	CGLGroup* tGroup = m_LogicData->GetGroup();
	if (nullptr == tGroup)
		return;

	for (int32_t i = 0; i != GroupCount; ++i) {
		AnimalLookInfo* tInfo = tGroup->GetMember((GroupIndex)i);
		if (nullptr != tInfo) {
			m_GroupList->pushBackCustomItem(CCommonUiUtil::initItemPersonLook(m_ListNode, i, tInfo));
		}
	}
}

void CLayerGroup::onFlushListGroup() {
	m_GroupList->removeAllItems();
	CLogicData::MapTempGroup_t& tTempGroups = m_LogicData->GetTempGroup();
	int32_t i = 0;
	for (CLogicData::MapTempGroup_t::iterator it = tTempGroups.begin(); it != tTempGroups.end(); ++it) {
		AnimalLookInfo* tInfo = &(it->second);
		m_GroupList->pushBackCustomItem(CCommonUiUtil::initItemPersonLook(m_ListNode, i, tInfo));
	}
}

void CLayerGroup::onFlushTempUser() {
	m_GroupList->removeAllItems();
	CGLGroup* tGroup = m_LogicData->GetGroup();
	if (nullptr == tGroup)
		return;

	CGLGroup::VecLook_t& tAll = tGroup->AllApply();
	for (size_t i = 0; i != tAll.size(); ++i) {
		AnimalLookInfo* tInfo = &tAll[i];

		m_GroupList->pushBackCustomItem(CCommonUiUtil::initItemPersonLook(m_ListNode, i, tInfo));
	}
}
